{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import quandl\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "quandl.ApiConfig.api_key = 'tAyfv1zpWnyhmDsp91yv'\n",
    "aapl_table = quandl.get('WIKI/AAPL')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "aapl = aapl_table['Adj. Close']['2017']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2017-01-03    114.715378\n",
      "2017-01-04    114.586983\n",
      "2017-01-05    115.169696\n",
      "2017-01-06    116.453639\n",
      "2017-01-09    117.520300\n",
      "2017-01-10    117.638817\n",
      "2017-01-11    118.270913\n",
      "2017-01-12    117.777088\n",
      "2017-01-13    117.569682\n",
      "2017-01-17    118.517825\n",
      "2017-01-18    118.507948\n",
      "2017-01-19    118.300542\n",
      "2017-01-20    118.517825\n",
      "2017-01-23    118.596837\n",
      "2017-01-24    118.488195\n",
      "2017-01-25    120.374604\n",
      "2017-01-26    120.433863\n",
      "2017-01-27    120.443739\n",
      "2017-01-30    120.127692\n",
      "2017-01-31    119.851150\n",
      "2017-02-01    127.159749\n",
      "2017-02-02    126.942467\n",
      "2017-02-03    127.485673\n",
      "2017-02-06    128.680728\n",
      "2017-02-07    129.905412\n",
      "2017-02-08    130.409113\n",
      "2017-02-09    131.347379\n",
      "2017-02-10    131.049809\n",
      "2017-02-13    132.210332\n",
      "2017-02-14    133.926319\n",
      "                 ...    \n",
      "2017-11-16    171.100000\n",
      "2017-11-17    170.150000\n",
      "2017-11-20    169.980000\n",
      "2017-11-21    173.140000\n",
      "2017-11-22    174.960000\n",
      "2017-11-24    174.970000\n",
      "2017-11-27    174.090000\n",
      "2017-11-28    173.070000\n",
      "2017-11-29    169.480000\n",
      "2017-11-30    171.850000\n",
      "2017-12-01    171.050000\n",
      "2017-12-04    169.800000\n",
      "2017-12-05    169.640000\n",
      "2017-12-06    169.010000\n",
      "2017-12-07    169.452000\n",
      "2017-12-08    169.370000\n",
      "2017-12-11    172.670000\n",
      "2017-12-12    171.700000\n",
      "2017-12-13    172.270000\n",
      "2017-12-14    172.220000\n",
      "2017-12-15    173.870000\n",
      "2017-12-18    176.420000\n",
      "2017-12-19    174.540000\n",
      "2017-12-20    174.350000\n",
      "2017-12-21    175.010000\n",
      "2017-12-22    175.010000\n",
      "2017-12-26    170.570000\n",
      "2017-12-27    170.600000\n",
      "2017-12-28    171.080000\n",
      "2017-12-29    169.230000\n",
      "Name: Adj. Close, Length: 249, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(aapl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2017-03-01    138.657681\n",
      "2017-03-02    137.834404\n",
      "2017-03-03    138.647762\n",
      "2017-03-06    138.211326\n",
      "2017-03-07    138.389868\n",
      "2017-03-08    137.874080\n",
      "2017-03-09    137.556672\n",
      "2017-03-10    138.012946\n",
      "2017-03-13    138.072460\n",
      "2017-03-14    137.864161\n",
      "2017-03-15    139.322254\n",
      "2017-03-16    139.550391\n",
      "2017-03-17    138.856061\n",
      "2017-03-20    140.314154\n",
      "2017-03-21    138.707276\n",
      "2017-03-22    140.274478\n",
      "2017-03-23    139.778528\n",
      "2017-03-24    139.500796\n",
      "2017-03-27    139.738852\n",
      "2017-03-28    142.635200\n",
      "2017-03-29    142.952608\n",
      "2017-03-30    142.764147\n",
      "2017-03-31    142.496334\n",
      "Name: Adj. Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(aapl['2017-3'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2017-02-01    127.159749\n",
       "2017-02-02    126.942467\n",
       "2017-02-03    127.485673\n",
       "2017-02-06    128.680728\n",
       "2017-02-07    129.905412\n",
       "2017-02-08    130.409113\n",
       "2017-02-09    131.347379\n",
       "2017-02-10    131.049809\n",
       "2017-02-13    132.210332\n",
       "2017-02-14    133.926319\n",
       "2017-02-15    134.412350\n",
       "2017-02-16    134.248686\n",
       "2017-02-17    134.620649\n",
       "2017-02-21    135.592711\n",
       "2017-02-22    135.999390\n",
       "2017-02-23    135.424088\n",
       "2017-02-24    135.553035\n",
       "2017-02-27    135.820848\n",
       "2017-02-28    135.880362\n",
       "2017-03-01    138.657681\n",
       "2017-03-02    137.834404\n",
       "2017-03-03    138.647762\n",
       "2017-03-06    138.211326\n",
       "2017-03-07    138.389868\n",
       "2017-03-08    137.874080\n",
       "2017-03-09    137.556672\n",
       "2017-03-10    138.012946\n",
       "2017-03-13    138.072460\n",
       "2017-03-14    137.864161\n",
       "2017-03-15    139.322254\n",
       "                 ...    \n",
       "2017-03-17    138.856061\n",
       "2017-03-20    140.314154\n",
       "2017-03-21    138.707276\n",
       "2017-03-22    140.274478\n",
       "2017-03-23    139.778528\n",
       "2017-03-24    139.500796\n",
       "2017-03-27    139.738852\n",
       "2017-03-28    142.635200\n",
       "2017-03-29    142.952608\n",
       "2017-03-30    142.764147\n",
       "2017-03-31    142.496334\n",
       "2017-04-03    142.536010\n",
       "2017-04-04    143.597342\n",
       "2017-04-05    142.853418\n",
       "2017-04-06    142.496334\n",
       "2017-04-07    142.178926\n",
       "2017-04-10    142.010303\n",
       "2017-04-11    140.482777\n",
       "2017-04-12    140.651400\n",
       "2017-04-13    139.907475\n",
       "2017-04-17    140.681157\n",
       "2017-04-18    140.056260\n",
       "2017-04-19    139.540472\n",
       "2017-04-20    141.286216\n",
       "2017-04-21    141.117593\n",
       "2017-04-24    142.476496\n",
       "2017-04-25    143.369205\n",
       "2017-04-26    142.487208\n",
       "2017-04-27    142.625281\n",
       "2017-04-28    142.486415\n",
       "Name: Adj. Close, Length: 61, dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aapl['2017-2':'2017-4']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2017-01-03    114.715378\n",
      "2017-01-04    114.586983\n",
      "2017-01-05    115.169696\n",
      "2017-01-06    116.453639\n",
      "2017-01-09    117.520300\n",
      "Name: Adj. Close, dtype: float64\n",
      "Date\n",
      "2017-12-15    173.87\n",
      "2017-12-18    176.42\n",
      "2017-12-19    174.54\n",
      "2017-12-20    174.35\n",
      "2017-12-21    175.01\n",
      "2017-12-22    175.01\n",
      "2017-12-26    170.57\n",
      "2017-12-27    170.60\n",
      "2017-12-28    171.08\n",
      "2017-12-29    169.23\n",
      "Name: Adj. Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(aapl.head(5))\n",
    "print(aapl.tail(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2017-01-31    118.093136\n",
      "2017-02-28    132.456268\n",
      "2017-03-31    139.478802\n",
      "2017-04-30    141.728436\n",
      "2017-05-31    151.386305\n",
      "2017-06-30    147.233064\n",
      "2017-07-31    147.706190\n",
      "2017-08-31    158.856375\n",
      "2017-09-30    157.606500\n",
      "2017-10-31    157.811627\n",
      "2017-11-30    172.214500\n",
      "2017-12-31    171.893100\n",
      "Freq: M, Name: Adj. Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "by_month = aapl.resample('M').mean()\n",
    "print(by_month)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2017-01-08    115.231424\n",
      "2017-01-15    117.755360\n",
      "2017-01-22    118.461035\n",
      "2017-01-29    119.667448\n",
      "2017-02-05    124.313346\n",
      "Freq: W-SUN, Name: Adj. Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "by_week = aapl.resample('W').mean()\n",
    "print(by_week.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2017-01-31    120.443739\n",
       "2017-02-28    135.999390\n",
       "2017-03-31    142.952608\n",
       "2017-04-30    143.597342\n",
       "2017-05-31    155.469192\n",
       "2017-06-30    154.821818\n",
       "2017-07-31    152.839860\n",
       "2017-08-31    164.000000\n",
       "2017-09-30    164.050000\n",
       "2017-10-31    169.040000\n",
       "2017-11-30    175.880000\n",
       "2017-12-31    176.420000\n",
       "Freq: M, Name: Adj. Close, dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aapl.resample('M').max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "three_day = aapl.resample('3D').mean()\n",
    "two_week = aapl.resample('2W').mean()\n",
    "two_month = aapl.resample('2M').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "std = aapl.resample('W').std()\n",
    "max = aapl.resample('W').max()\n",
    "min = aapl.resample('W').min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2017-01-31    0.045940\n",
      "2017-02-28    0.070409\n",
      "2017-03-31    0.033823\n",
      "2017-04-30   -0.007736\n",
      "2017-05-31    0.039829\n",
      "2017-06-30   -0.073528\n",
      "2017-07-31    0.033035\n",
      "2017-08-31    0.047890\n",
      "2017-09-30   -0.049112\n",
      "2017-10-31    0.094252\n",
      "2017-11-30    0.022247\n",
      "2017-12-31   -0.003357\n",
      "Freq: M, Name: Adj. Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "monthly_return = aapl.resample('M').agg(lambda x: x[-1]/x[1] - 1)\n",
    "print(monthly_return)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2017-01-31    119.851150\n",
      "2017-02-28    135.880362\n",
      "2017-03-31    142.496334\n",
      "2017-04-30    142.486415\n",
      "2017-05-31    152.142689\n",
      "2017-06-30    143.438008\n",
      "2017-07-31    148.248489\n",
      "2017-08-31    164.000000\n",
      "2017-09-30    154.120000\n",
      "2017-10-31    169.040000\n",
      "2017-11-30    171.850000\n",
      "2017-12-31    169.230000\n",
      "Freq: M, Name: Adj. Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "last_day = aapl.resample('M').agg(lambda x: x[-1])\n",
    "print(last_day)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "by_week = aapl.resample('W').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2017-01-31    1.781512\n",
       "2017-03-31    4.323834\n",
       "2017-05-31    5.454495\n",
       "2017-07-31    3.865333\n",
       "2017-09-30    3.634223\n",
       "2017-11-30    8.011704\n",
       "2018-01-31    2.291229\n",
       "Freq: 2M, Name: Adj. Close, dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "twomon = aapl.resample('2M').std()\n",
    "twomon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2017-01-31    119.851150\n",
       "2017-02-28    135.880362\n",
       "2017-03-31    142.496334\n",
       "2017-04-30    142.486415\n",
       "2017-05-31    152.142689\n",
       "2017-06-30    143.438008\n",
       "2017-07-31    148.248489\n",
       "2017-08-31    164.000000\n",
       "2017-09-30    154.120000\n",
       "2017-10-31    169.040000\n",
       "2017-11-30    171.850000\n",
       "2017-12-31    169.230000\n",
       "Freq: M, Name: Adj. Close, dtype: float64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "last_day = aapl.resample('M').agg(lambda x: x[-1])\n",
    "last_day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2017-01-31    0.045940\n",
       "2017-02-28    0.070409\n",
       "2017-03-31    0.033823\n",
       "2017-04-30   -0.007736\n",
       "2017-05-31    0.039829\n",
       "2017-06-30   -0.073528\n",
       "2017-07-31    0.033035\n",
       "2017-08-31    0.047890\n",
       "2017-09-30   -0.049112\n",
       "2017-10-31    0.094252\n",
       "2017-11-30    0.022247\n",
       "2017-12-31   -0.003357\n",
       "Freq: M, Name: Adj. Close, dtype: float64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "monthly_return = aapl.resample('M').agg(lambda x: x[-1]/x[1] - 1)\n",
    "monthly_return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.02114094011940022\n",
      "0.04775652864223314\n",
      "0.09425168306576914\n"
     ]
    }
   ],
   "source": [
    "print(monthly_return.mean())\n",
    "print(monthly_return.std())\n",
    "print(monthly_return.max())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2017-01-31          NaN\n",
      "2017-02-28    16.029211\n",
      "2017-03-31     6.615972\n",
      "2017-04-30    -0.009919\n",
      "2017-05-31     9.656274\n",
      "2017-06-30    -8.704681\n",
      "2017-07-31     4.810482\n",
      "2017-08-31    15.751511\n",
      "2017-09-30    -9.880000\n",
      "2017-10-31    14.920000\n",
      "2017-11-30     2.810000\n",
      "2017-12-31    -2.620000\n",
      "Freq: M, Name: Adj. Close, dtype: float64\n",
      "Date\n",
      "2017-01-31         NaN\n",
      "2017-02-28    0.133743\n",
      "2017-03-31    0.048690\n",
      "2017-04-30   -0.000070\n",
      "2017-05-31    0.067770\n",
      "2017-06-30   -0.057214\n",
      "2017-07-31    0.033537\n",
      "2017-08-31    0.106251\n",
      "2017-09-30   -0.060244\n",
      "2017-10-31    0.096808\n",
      "2017-11-30    0.016623\n",
      "2017-12-31   -0.015246\n",
      "Freq: M, Name: Adj. Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(last_day.diff())\n",
    "print(last_day.pct_change())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count    249.000000\n",
      "mean     149.815713\n",
      "std       15.065681\n",
      "min      114.586983\n",
      "25%      140.651400\n",
      "50%      151.890000\n",
      "75%      159.780000\n",
      "max      176.420000\n",
      "Name: Adj. Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(aapl.describe())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2017-01-31    0.000000\n",
      "2017-02-28    0.133743\n",
      "2017-03-31    0.048690\n",
      "2017-04-30   -0.000070\n",
      "2017-05-31    0.067770\n",
      "2017-06-30   -0.057214\n",
      "2017-07-31    0.033537\n",
      "2017-08-31    0.106251\n",
      "2017-09-30   -0.060244\n",
      "2017-10-31    0.096808\n",
      "2017-11-30    0.016623\n",
      "2017-12-31   -0.015246\n",
      "Freq: M, Name: Adj. Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "daily_return = last_day.pct_change()\n",
    "print(daily_return.fillna(0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2017-01-31    0.133743\n",
      "2017-02-28    0.133743\n",
      "2017-03-31    0.048690\n",
      "2017-04-30   -0.000070\n",
      "2017-05-31    0.067770\n",
      "2017-06-30   -0.057214\n",
      "2017-07-31    0.033537\n",
      "2017-08-31    0.106251\n",
      "2017-09-30   -0.060244\n",
      "2017-10-31    0.096808\n",
      "2017-11-30    0.016623\n",
      "2017-12-31   -0.015246\n",
      "Freq: M, Name: Adj. Close, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "daily_return = last_day.pct_change()\n",
    "print(daily_return.fillna(method = 'bfill'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2017-02-28    0.133743\n",
       "2017-03-31    0.048690\n",
       "2017-04-30   -0.000070\n",
       "2017-05-31    0.067770\n",
       "2017-06-30   -0.057214\n",
       "2017-07-31    0.033537\n",
       "2017-08-31    0.106251\n",
       "2017-09-30   -0.060244\n",
       "2017-10-31    0.096808\n",
       "2017-11-30    0.016623\n",
       "2017-12-31   -0.015246\n",
       "Freq: M, Name: Adj. Close, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "daily_return = last_day.pct_change()\n",
    "daily_return.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# aapl.resample('M').agg(lambda x: max(x) - min(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              AAPL    GOOG     IBM\n",
      "2017-07-03  143.50  898.70  155.58\n",
      "2017-07-04  144.09  911.71  153.67\n",
      "2017-07-05  142.73  906.69  152.36\n",
      "2017-07-06  144.18  918.59  152.94\n",
      "2017-07-07  143.77  926.99  153.49\n"
     ]
    }
   ],
   "source": [
    "dict = {'AAPL': [143.5, 144.09, 142.73, 144.18, 143.77],'GOOG':[898.7, 911.71, 906.69, 918.59, 926.99],\n",
    "        'IBM':[155.58, 153.67, 152.36, 152.94, 153.49]}\n",
    "data_index = pd.date_range('2017-07-03',periods = 5, freq = 'D')\n",
    "df = pd.DataFrame(dict, index = data_index)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     AAPL    GOOG\n",
      "0  143.50  898.70\n",
      "1  144.09  911.71\n",
      "2  142.73  906.69\n",
      "3  144.18  918.59\n",
      "4  143.77  926.99\n"
     ]
    }
   ],
   "source": [
    "s1 = pd.Series([143.5, 144.09, 142.73, 144.18, 143.77], name = 'AAPL')\n",
    "s2 = pd.Series([898.7, 911.71, 906.69, 918.59, 926.99], name = 'GOOG')\n",
    "data_frame = pd.concat([s1,s2], axis = 1)\n",
    "print(data_frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['AAPL', 'GOOG', 'IBM'], dtype='object')\n",
      "2017-07-03    143.50\n",
      "2017-07-04    144.09\n",
      "2017-07-05    142.73\n",
      "2017-07-06    144.18\n",
      "2017-07-07    143.77\n",
      "Freq: D, Name: AAPL, dtype: float64\n",
      "2017-07-03    898.70\n",
      "2017-07-04    911.71\n",
      "2017-07-05    906.69\n",
      "2017-07-06    918.59\n",
      "2017-07-07    926.99\n",
      "Freq: D, Name: GOOG, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(df.columns)\n",
    "print(df.AAPL)\n",
    "print(df['GOOG'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2018-03-21    171.270\n",
      "2018-03-22    168.845\n",
      "2018-03-23    164.940\n",
      "2018-03-26    172.770\n",
      "2018-03-27    168.340\n",
      "Name: Close, dtype: float64\n",
      "Date\n",
      "2018-03-21    35247358.0\n",
      "2018-03-22    41051076.0\n",
      "2018-03-23    40248954.0\n",
      "2018-03-26    36272617.0\n",
      "2018-03-27    38962839.0\n",
      "Name: Adj. Volume, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "df = aapl_table\n",
    "print(df.Close.tail(5))\n",
    "print(df['Adj. Volume'].tail(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Open      High     Low   Close      Volume  Ex-Dividend  \\\n",
      "Date                                                                    \n",
      "2016-01-31   94.79   97.3400   94.35   97.34  64416504.0          0.0   \n",
      "2016-02-29   96.86   98.2300   96.65   96.69  35216277.0          0.0   \n",
      "2016-03-31  109.72  109.9000  108.88  108.99  25888449.0          0.0   \n",
      "2016-04-30   93.99   94.7200   92.51   93.74  68531478.0          0.0   \n",
      "2016-05-31   99.60  100.4000   98.82   99.86  42307212.0          0.0   \n",
      "2016-06-30   94.44   95.7700   94.30   95.60  35836356.0          0.0   \n",
      "2016-07-31  104.19  104.5500  103.68  104.21  27733688.0          0.0   \n",
      "2016-08-31  105.66  106.5699  105.64  106.10  29662406.0          0.0   \n",
      "2016-09-30  112.46  113.3700  111.80  113.05  36379106.0          0.0   \n",
      "2016-10-31  113.65  114.2300  113.20  113.54  26419398.0          0.0   \n",
      "2016-11-30  111.56  112.2000  110.27  110.52  36162258.0          0.0   \n",
      "2016-12-31  116.65  117.2000  115.43  115.82  30586265.0          0.0   \n",
      "\n",
      "            Split Ratio   Adj. Open   Adj. High    Adj. Low  Adj. Close  \\\n",
      "Date                                                                      \n",
      "2016-01-31          1.0   91.581233   94.044912   91.156128   94.044912   \n",
      "2016-02-29          1.0   94.084911   95.415659   93.880927   93.919781   \n",
      "2016-03-31          1.0  106.576465  106.751308  105.760531  105.867380   \n",
      "2016-04-30          1.0   91.297138   92.006223   89.859540   91.054300   \n",
      "2016-05-31          1.0   97.337844   98.119674   96.575559   97.591939   \n",
      "2016-06-30          1.0   92.295040   93.594832   92.158220   93.428693   \n",
      "2016-07-31          1.0  101.823594  102.175417  101.325177  101.843140   \n",
      "2016-08-31          1.0  103.816156  104.710177  103.796505  104.248477   \n",
      "2016-09-30          1.0  110.497491  111.391610  109.849008  111.077195   \n",
      "2016-10-31          1.0  111.666724  112.236603  111.224577  111.558644   \n",
      "2016-11-30          1.0  110.182071  110.814166  108.908004  109.154917   \n",
      "2016-12-31          1.0  115.209202  115.752409  114.004271  114.389454   \n",
      "\n",
      "            Adj. Volume  \n",
      "Date                     \n",
      "2016-01-31   64416504.0  \n",
      "2016-02-29   35216277.0  \n",
      "2016-03-31   25888449.0  \n",
      "2016-04-30   68531478.0  \n",
      "2016-05-31   42307212.0  \n",
      "2016-06-30   35836356.0  \n",
      "2016-07-31   27733688.0  \n",
      "2016-08-31   29662406.0  \n",
      "2016-09-30   36379106.0  \n",
      "2016-10-31   26419398.0  \n",
      "2016-11-30   36162258.0  \n",
      "2016-12-31   30586265.0  \n"
     ]
    }
   ],
   "source": [
    "aapl_2016 = df['2016']\n",
    "aapl_month = aapl_2016.resample('M').agg(lambda x: x[-1])\n",
    "print(aapl_month)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Open      High     Low   Close\n",
      "Date                                        \n",
      "2016-01-31   94.79   97.3400   94.35   97.34\n",
      "2016-02-29   96.86   98.2300   96.65   96.69\n",
      "2016-03-31  109.72  109.9000  108.88  108.99\n",
      "2016-04-30   93.99   94.7200   92.51   93.74\n",
      "2016-05-31   99.60  100.4000   98.82   99.86\n",
      "2016-06-30   94.44   95.7700   94.30   95.60\n",
      "2016-07-31  104.19  104.5500  103.68  104.21\n",
      "2016-08-31  105.66  106.5699  105.64  106.10\n",
      "2016-09-30  112.46  113.3700  111.80  113.05\n",
      "2016-10-31  113.65  114.2300  113.20  113.54\n",
      "2016-11-30  111.56  112.2000  110.27  110.52\n",
      "2016-12-31  116.65  117.2000  115.43  115.82\n"
     ]
    }
   ],
   "source": [
    "aapl_bar = aapl_month[['Open', 'High', 'Low', 'Close']]\n",
    "print(aapl_bar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Open    High     Low   Close\n",
      "Date                                      \n",
      "2016-03-31  109.72  109.90  108.88  108.99\n",
      "2016-04-30   93.99   94.72   92.51   93.74\n",
      "2016-05-31   99.60  100.40   98.82   99.86\n",
      "2016-06-30   94.44   95.77   94.30   95.60\n"
     ]
    }
   ],
   "source": [
    "print(aapl_month.loc['2016-03':'2016-06',['Open', 'High', 'Low', 'Close']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Open      High     Low   Close\n",
      "Date                                        \n",
      "2016-03-31  109.72  109.9000  108.88  108.99\n",
      "2016-08-31  105.66  106.5699  105.64  106.10\n",
      "2016-09-30  112.46  113.3700  111.80  113.05\n",
      "2016-10-31  113.65  114.2300  113.20  113.54\n",
      "2016-11-30  111.56  112.2000  110.27  110.52\n",
      "2016-12-31  116.65  117.2000  115.43  115.82\n"
     ]
    }
   ],
   "source": [
    "above = aapl_bar[aapl_bar.Close > np.mean(aapl_bar.Close)]\n",
    "print(above)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Open      High     Low   Close  rate_return\n",
      "Date                                                     \n",
      "2016-01-31   94.79   97.3400   94.35   97.34          NaN\n",
      "2016-02-29   96.86   98.2300   96.65   96.69    -0.006678\n",
      "2016-03-31  109.72  109.9000  108.88  108.99     0.127211\n",
      "2016-04-30   93.99   94.7200   92.51   93.74    -0.139921\n",
      "2016-05-31   99.60  100.4000   98.82   99.86     0.065287\n",
      "2016-06-30   94.44   95.7700   94.30   95.60    -0.042660\n",
      "2016-07-31  104.19  104.5500  103.68  104.21     0.090063\n",
      "2016-08-31  105.66  106.5699  105.64  106.10     0.018136\n",
      "2016-09-30  112.46  113.3700  111.80  113.05     0.065504\n",
      "2016-10-31  113.65  114.2300  113.20  113.54     0.004334\n",
      "2016-11-30  111.56  112.2000  110.27  110.52    -0.026599\n",
      "2016-12-31  116.65  117.2000  115.43  115.82     0.047955\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "aapl_bar['rate_return'] = aapl_bar.Close.pct_change()\n",
    "print(aapl_bar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Open   High    Low  Close  rate_return\n",
      "Date                                               \n",
      "2016-01-31  False  False  False  False         True\n",
      "2016-02-29  False  False  False  False        False\n",
      "2016-03-31  False  False  False  False        False\n",
      "2016-04-30  False  False  False  False        False\n",
      "2016-05-31  False  False  False  False        False\n",
      "2016-06-30  False  False  False  False        False\n",
      "2016-07-31  False  False  False  False        False\n",
      "2016-08-31  False  False  False  False        False\n",
      "2016-09-30  False  False  False  False        False\n",
      "2016-10-31  False  False  False  False        False\n",
      "2016-11-30  False  False  False  False        False\n",
      "2016-12-31  False  False  False  False        False\n",
      "\n",
      "------------------ separate line -----------------\n",
      "\n",
      "         Open   High    Low  Close rate_return\n",
      "count      12     12     12     12          12\n",
      "unique      1      1      1      1           2\n",
      "top     False  False  False  False       False\n",
      "freq       12     12     12     12          11\n"
     ]
    }
   ],
   "source": [
    "missing = aapl_bar.isnull()\n",
    "print(missing)\n",
    "print('\\n------------------ separate line -----------------\\n')\n",
    "print(missing.describe())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Open   High    Low  Close  rate_return\n",
      "Date                                               \n",
      "2016-01-31  False  False  False  False         True\n"
     ]
    }
   ],
   "source": [
    "print(missing[missing.rate_return == True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Open      High     Low   Close  rate_return\n",
      "Date                                                     \n",
      "2016-02-29   96.86   98.2300   96.65   96.69    -0.006678\n",
      "2016-03-31  109.72  109.9000  108.88  108.99     0.127211\n",
      "2016-04-30   93.99   94.7200   92.51   93.74    -0.139921\n",
      "2016-05-31   99.60  100.4000   98.82   99.86     0.065287\n",
      "2016-06-30   94.44   95.7700   94.30   95.60    -0.042660\n",
      "2016-07-31  104.19  104.5500  103.68  104.21     0.090063\n",
      "2016-08-31  105.66  106.5699  105.64  106.10     0.018136\n",
      "2016-09-30  112.46  113.3700  111.80  113.05     0.065504\n",
      "2016-10-31  113.65  114.2300  113.20  113.54     0.004334\n",
      "2016-11-30  111.56  112.2000  110.27  110.52    -0.026599\n",
      "2016-12-31  116.65  117.2000  115.43  115.82     0.047955\n",
      "\n",
      "---------------------- separate line--------------------\n",
      "\n",
      "              Open      High     Low   Close  rate_return\n",
      "Date                                                     \n",
      "2016-01-31   94.79   97.3400   94.35   97.34     0.000000\n",
      "2016-02-29   96.86   98.2300   96.65   96.69    -0.006678\n",
      "2016-03-31  109.72  109.9000  108.88  108.99     0.127211\n",
      "2016-04-30   93.99   94.7200   92.51   93.74    -0.139921\n",
      "2016-05-31   99.60  100.4000   98.82   99.86     0.065287\n",
      "2016-06-30   94.44   95.7700   94.30   95.60    -0.042660\n",
      "2016-07-31  104.19  104.5500  103.68  104.21     0.090063\n",
      "2016-08-31  105.66  106.5699  105.64  106.10     0.018136\n",
      "2016-09-30  112.46  113.3700  111.80  113.05     0.065504\n",
      "2016-10-31  113.65  114.2300  113.20  113.54     0.004334\n",
      "2016-11-30  111.56  112.2000  110.27  110.52    -0.026599\n",
      "2016-12-31  116.65  117.2000  115.43  115.82     0.047955\n"
     ]
    }
   ],
   "source": [
    "drop = aapl_bar.dropna()\n",
    "print(drop)\n",
    "print('\\n---------------------- separate line--------------------\\n')\n",
    "fill = aapl_bar.fillna(0)\n",
    "print(fill)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     AAPL    GOOG\n",
      "0  143.50  898.70\n",
      "1  144.09  911.71\n",
      "2  142.73  906.69\n",
      "3  144.18  918.59\n",
      "4  143.77  926.99\n"
     ]
    }
   ],
   "source": [
    "s1 = pd.Series([143.5, 144.09, 142.73, 144.18, 143.77], name = 'AAPL')\n",
    "s2 = pd.Series([898.7, 911.71, 906.69, 918.59, 926.99], name = 'GOOG')\n",
    "data_frame = pd.concat([s1,s2], axis = 1)\n",
    "print(data_frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date\n",
      "2016-01-31    4.578210\n",
      "2016-02-29    4.571510\n",
      "2016-03-31    4.691256\n",
      "2016-04-30    4.540525\n",
      "2016-05-31    4.603769\n",
      "2016-06-30    4.560173\n",
      "2016-07-31    4.646408\n",
      "2016-08-31    4.664382\n",
      "2016-09-30    4.727830\n",
      "2016-10-31    4.732155\n",
      "2016-11-30    4.705197\n",
      "2016-12-31    4.752037\n",
      "Freq: M, Name: log_price, dtype: float64\n",
      "\n",
      "---------------------- separate line--------------------\n",
      "\n",
      "              Open      High     Low   Close  rate_return  log_price\n",
      "Date                                                                \n",
      "2016-01-31   94.79   97.3400   94.35   97.34          NaN   4.578210\n",
      "2016-02-29   96.86   98.2300   96.65   96.69    -0.006678   4.571510\n",
      "2016-03-31  109.72  109.9000  108.88  108.99     0.127211   4.691256\n",
      "2016-04-30   93.99   94.7200   92.51   93.74    -0.139921   4.540525\n",
      "2016-05-31   99.60  100.4000   98.82   99.86     0.065287   4.603769\n",
      "2016-06-30   94.44   95.7700   94.30   95.60    -0.042660   4.560173\n",
      "2016-07-31  104.19  104.5500  103.68  104.21     0.090063   4.646408\n",
      "2016-08-31  105.66  106.5699  105.64  106.10     0.018136   4.664382\n",
      "2016-09-30  112.46  113.3700  111.80  113.05     0.065504   4.727830\n",
      "2016-10-31  113.65  114.2300  113.20  113.54     0.004334   4.732155\n",
      "2016-11-30  111.56  112.2000  110.27  110.52    -0.026599   4.705197\n",
      "2016-12-31  116.65  117.2000  115.43  115.82     0.047955   4.752037\n"
     ]
    }
   ],
   "source": [
    "log_price = np.log(aapl_bar.Close)\n",
    "log_price.name = 'log_price'\n",
    "print(log_price)\n",
    "print('\\n---------------------- separate line--------------------\\n')\n",
    "concat = pd.concat([aapl_bar, log_price], axis = 1)\n",
    "print(concat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                Volume  Split Ratio\n",
      "Date                               \n",
      "2016-10-31  26419398.0          1.0\n",
      "2016-11-30  36162258.0          1.0\n",
      "2016-12-31  30586265.0          1.0\n",
      "2017-01-31  49200993.0          1.0\n",
      "2017-02-28  23482860.0          1.0\n",
      "2017-03-31  19661651.0          1.0\n",
      "2017-04-30  20247187.0          1.0\n",
      "\n",
      "---------------------- separate line--------------------\n",
      "\n",
      "              Open     High     Low   Close\n",
      "Date                                       \n",
      "2016-10-31  113.65  114.230  113.20  113.54\n",
      "2016-11-30  111.56  112.200  110.27  110.52\n",
      "2016-12-31  116.65  117.200  115.43  115.82\n",
      "2017-01-31  121.15  121.390  120.62  121.35\n",
      "2017-02-28  137.08  137.435  136.70  136.99\n",
      "2017-03-31  143.72  144.270  143.01  143.66\n",
      "2017-04-30  144.09  144.300  143.27  143.65\n"
     ]
    }
   ],
   "source": [
    "df_volume = aapl_table.loc['2016-10':'2017-04',['Volume', 'Split Ratio']].resample('M').agg(lambda x: x[-1])\n",
    "print(df_volume)\n",
    "print('\\n---------------------- separate line--------------------\\n')\n",
    "df_2017 = aapl_table.loc['2016-10':'2017-04',['Open', 'High', 'Low', 'Close']].resample('M').agg(lambda x: x[-1])\n",
    "print(df_2017)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Open      High     Low   Close  rate_return      Volume  \\\n",
      "Date                                                                    \n",
      "2016-01-31   94.79   97.3400   94.35   97.34          NaN         NaN   \n",
      "2016-02-29   96.86   98.2300   96.65   96.69    -0.006678         NaN   \n",
      "2016-03-31  109.72  109.9000  108.88  108.99     0.127211         NaN   \n",
      "2016-04-30   93.99   94.7200   92.51   93.74    -0.139921         NaN   \n",
      "2016-05-31   99.60  100.4000   98.82   99.86     0.065287         NaN   \n",
      "2016-06-30   94.44   95.7700   94.30   95.60    -0.042660         NaN   \n",
      "2016-07-31  104.19  104.5500  103.68  104.21     0.090063         NaN   \n",
      "2016-08-31  105.66  106.5699  105.64  106.10     0.018136         NaN   \n",
      "2016-09-30  112.46  113.3700  111.80  113.05     0.065504         NaN   \n",
      "2016-10-31  113.65  114.2300  113.20  113.54     0.004334  26419398.0   \n",
      "2016-11-30  111.56  112.2000  110.27  110.52    -0.026599  36162258.0   \n",
      "2016-12-31  116.65  117.2000  115.43  115.82     0.047955  30586265.0   \n",
      "2017-01-31     NaN       NaN     NaN     NaN          NaN  49200993.0   \n",
      "2017-02-28     NaN       NaN     NaN     NaN          NaN  23482860.0   \n",
      "2017-03-31     NaN       NaN     NaN     NaN          NaN  19661651.0   \n",
      "2017-04-30     NaN       NaN     NaN     NaN          NaN  20247187.0   \n",
      "\n",
      "            Split Ratio  \n",
      "Date                     \n",
      "2016-01-31          NaN  \n",
      "2016-02-29          NaN  \n",
      "2016-03-31          NaN  \n",
      "2016-04-30          NaN  \n",
      "2016-05-31          NaN  \n",
      "2016-06-30          NaN  \n",
      "2016-07-31          NaN  \n",
      "2016-08-31          NaN  \n",
      "2016-09-30          NaN  \n",
      "2016-10-31          1.0  \n",
      "2016-11-30          1.0  \n",
      "2016-12-31          1.0  \n",
      "2017-01-31          1.0  \n",
      "2017-02-28          1.0  \n",
      "2017-03-31          1.0  \n",
      "2017-04-30          1.0  \n"
     ]
    }
   ],
   "source": [
    "concat = pd.concat([aapl_bar,df_volume],axis = 1)\n",
    "print(concat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Open    High     Low   Close  rate_return      Volume  \\\n",
      "Date                                                                  \n",
      "2016-10-31  113.65  114.23  113.20  113.54     0.004334  26419398.0   \n",
      "2016-11-30  111.56  112.20  110.27  110.52    -0.026599  36162258.0   \n",
      "2016-12-31  116.65  117.20  115.43  115.82     0.047955  30586265.0   \n",
      "\n",
      "            Split Ratio  \n",
      "Date                     \n",
      "2016-10-31          1.0  \n",
      "2016-11-30          1.0  \n",
      "2016-12-31          1.0  \n"
     ]
    }
   ],
   "source": [
    "concat = pd.concat([aapl_bar,df_volume],axis = 1, join = 'inner')\n",
    "print(concat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Close      High     Low    Open  rate_return\n",
      "Date                                                     \n",
      "2016-01-31   97.34   97.3400   94.35   94.79          NaN\n",
      "2016-02-29   96.69   98.2300   96.65   96.86    -0.006678\n",
      "2016-03-31  108.99  109.9000  108.88  109.72     0.127211\n",
      "2016-04-30   93.74   94.7200   92.51   93.99    -0.139921\n",
      "2016-05-31   99.86  100.4000   98.82   99.60     0.065287\n",
      "2016-06-30   95.60   95.7700   94.30   94.44    -0.042660\n",
      "2016-07-31  104.21  104.5500  103.68  104.19     0.090063\n",
      "2016-08-31  106.10  106.5699  105.64  105.66     0.018136\n",
      "2016-09-30  113.05  113.3700  111.80  112.46     0.065504\n",
      "2016-10-31  113.54  114.2300  113.20  113.65     0.004334\n",
      "2016-11-30  110.52  112.2000  110.27  111.56    -0.026599\n",
      "2016-12-31  115.82  117.2000  115.43  116.65     0.047955\n",
      "2016-10-31  113.54  114.2300  113.20  113.65          NaN\n",
      "2016-11-30  110.52  112.2000  110.27  111.56          NaN\n",
      "2016-12-31  115.82  117.2000  115.43  116.65          NaN\n",
      "2017-01-31  121.35  121.3900  120.62  121.15          NaN\n",
      "2017-02-28  136.99  137.4350  136.70  137.08          NaN\n",
      "2017-03-31  143.66  144.2700  143.01  143.72          NaN\n",
      "2017-04-30  143.65  144.3000  143.27  144.09          NaN\n"
     ]
    }
   ],
   "source": [
    "append = aapl_bar.append(df_2017)\n",
    "print(append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Close      High     Low    Open  rate_return\n",
      "Date                                                     \n",
      "2016-01-31   97.34   97.3400   94.35   94.79          NaN\n",
      "2016-02-29   96.69   98.2300   96.65   96.86    -0.006678\n",
      "2016-03-31  108.99  109.9000  108.88  109.72     0.127211\n",
      "2016-04-30   93.74   94.7200   92.51   93.99    -0.139921\n",
      "2016-05-31   99.86  100.4000   98.82   99.60     0.065287\n",
      "2016-06-30   95.60   95.7700   94.30   94.44    -0.042660\n",
      "2016-07-31  104.21  104.5500  103.68  104.19     0.090063\n",
      "2016-08-31  106.10  106.5699  105.64  105.66     0.018136\n",
      "2016-09-30  113.05  113.3700  111.80  112.46     0.065504\n",
      "2016-10-31  113.54  114.2300  113.20  113.65     0.004334\n",
      "2016-11-30  110.52  112.2000  110.27  111.56    -0.026599\n",
      "2016-12-31  115.82  117.2000  115.43  116.65     0.047955\n",
      "2016-10-31  113.54  114.2300  113.20  113.65          NaN\n",
      "2016-11-30  110.52  112.2000  110.27  111.56          NaN\n",
      "2016-12-31  115.82  117.2000  115.43  116.65          NaN\n",
      "2017-01-31  121.35  121.3900  120.62  121.15          NaN\n",
      "2017-02-28  136.99  137.4350  136.70  137.08          NaN\n",
      "2017-03-31  143.66  144.2700  143.01  143.72          NaN\n",
      "2017-04-30  143.65  144.3000  143.27  144.09          NaN\n"
     ]
    }
   ],
   "source": [
    "concat = pd.concat([aapl_bar, df_2017], axis = 0)\n",
    "print(concat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            Change   Close      High     Low    Open  rate_return\n",
      "Date                                                             \n",
      "2016-01-31     NaN   97.34   97.3400   94.35   94.79          NaN\n",
      "2016-02-29     NaN   96.69   98.2300   96.65   96.86    -0.006678\n",
      "2016-03-31     NaN  108.99  109.9000  108.88  109.72     0.127211\n",
      "2016-04-30     NaN   93.74   94.7200   92.51   93.99    -0.139921\n",
      "2016-05-31     NaN   99.86  100.4000   98.82   99.60     0.065287\n",
      "2016-06-30     NaN   95.60   95.7700   94.30   94.44    -0.042660\n",
      "2016-07-31     NaN  104.21  104.5500  103.68  104.19     0.090063\n",
      "2016-08-31     NaN  106.10  106.5699  105.64  105.66     0.018136\n",
      "2016-09-30     NaN  113.05  113.3700  111.80  112.46     0.065504\n",
      "2016-10-31     NaN  113.54  114.2300  113.20  113.65     0.004334\n",
      "2016-11-30     NaN  110.52  112.2000  110.27  111.56    -0.026599\n",
      "2016-12-31     NaN  115.82  117.2000  115.43  116.65     0.047955\n",
      "2016-10-31  113.65  113.54  114.2300  113.20     NaN          NaN\n",
      "2016-11-30  111.56  110.52  112.2000  110.27     NaN          NaN\n",
      "2016-12-31  116.65  115.82  117.2000  115.43     NaN          NaN\n",
      "2017-01-31  121.15  121.35  121.3900  120.62     NaN          NaN\n",
      "2017-02-28  137.08  136.99  137.4350  136.70     NaN          NaN\n",
      "2017-03-31  143.72  143.66  144.2700  143.01     NaN          NaN\n",
      "2017-04-30  144.09  143.65  144.3000  143.27     NaN          NaN\n"
     ]
    }
   ],
   "source": [
    "df_2017.columns = ['Change', 'High','Low','Close']\n",
    "concat = pd.concat([aapl_bar, df_2017], axis = 0)\n",
    "print(concat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            Change   Close      High     Low    Open  rate_return\n",
      "Date                                                             \n",
      "2016-01-31     NaN   97.34   97.3400   94.35   94.79          NaN\n",
      "2016-02-29     NaN   96.69   98.2300   96.65   96.86    -0.006678\n",
      "2016-03-31     NaN  108.99  109.9000  108.88  109.72     0.127211\n",
      "2016-04-30     NaN   93.74   94.7200   92.51   93.99    -0.139921\n",
      "2016-05-31     NaN   99.86  100.4000   98.82   99.60     0.065287\n",
      "2016-06-30     NaN   95.60   95.7700   94.30   94.44    -0.042660\n",
      "2016-07-31     NaN  104.21  104.5500  103.68  104.19     0.090063\n",
      "2016-08-31     NaN  106.10  106.5699  105.64  105.66     0.018136\n",
      "2016-09-30     NaN  113.05  113.3700  111.80  112.46     0.065504\n",
      "2016-10-31     NaN  113.54  114.2300  113.20  113.65     0.004334\n",
      "2016-11-30     NaN  110.52  112.2000  110.27  111.56    -0.026599\n",
      "2016-12-31     NaN  115.82  117.2000  115.43  116.65     0.047955\n",
      "2016-10-31  113.65  113.54  114.2300  113.20     NaN          NaN\n",
      "2016-11-30  111.56  110.52  112.2000  110.27     NaN          NaN\n",
      "2016-12-31  116.65  115.82  117.2000  115.43     NaN          NaN\n",
      "2017-01-31  121.15  121.35  121.3900  120.62     NaN          NaN\n",
      "2017-02-28  137.08  136.99  137.4350  136.70     NaN          NaN\n",
      "2017-03-31  143.72  143.66  144.2700  143.01     NaN          NaN\n",
      "2017-04-30  144.09  143.65  144.3000  143.27     NaN          NaN\n"
     ]
    }
   ],
   "source": [
    "concat = pd.concat([aapl_bar, df_2017], axis = 0)\n",
    "print(concat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
